<?php
/**
 *	作者：耿鸿飞
 *	日期：2011年10月31日
 *	作用：自动跳转的类
 **/

class GhfRun{
	private $_action_dir = '';
	private $_templates_dir = '';
	private $_templates_cache_dir = '';
	private $_plugins = array();
	private $_host = null;//数据库db地址
	private $_dbname = null;//数据库名字
	private $_user = null;//数据库用户名
	private $_pass = null;//数据库密码
	private $_charset = null;//数据库中的字符编码
	private $_pcon = false;//数据是否要长连接

	public $Control = '';
	public $Action = '';
	public $BasePath = '';
	function GhfRun(){
		$base = realpath(dirname(__FILE__))."/";
		$setpath = array("","Class","Include",'Smarty3','Plugins');
		set_include_path(get_include_path().implode(PATH_SEPARATOR.$base,$setpath));
		//echo get_include_path();
		spl_autoload_extensions(".cls.php,.class.php,.php"); 
		spl_autoload_register();
		header("Content-type: text/html; charset=utf-8");
	}
	/**
	 *	运行对应的action
	 **/
	public function run(){
		if(empty($this->_action_dir)){
			 echo "没有设置Action路径";die();
		}elseif(empty($this->_templates_dir)){
			 echo "没有设置模板路径";die();
		}elseif(empty($this->_templates_cache_dir)){
			 echo "没有设置模板缓存路径";die();
		}
		$smarty = new Smarty;
		$smarty->template_dir = $this->_templates_dir;
		$smarty->compile_dir = $this->_templates_cache_dir;		
		$smarty->plugins_dir = realpath(dirname(__FILE__))."/Smaerty3/UserPlugins";	
		$smarty->config_dir = realpath(dirname(__FILE__)).'/../templates/config/';
		
		$_ENV['smarty'] = $smarty;		
			
		$_ENV['smarty'] = $smarty;
		$_ENV['db'] = new G_db($this->_host,$this->_dbname,$this->_user,$this->_pass,$this->_charset,$this->_pcon);
		new LoadFunction;

		$url = $_SERVER["REQUEST_URI"];
		$basename = pathinfo($_SERVER["SCRIPT_NAME"]);//var_dump($basename);
		$this->BasePath = $basename['dirname'];
		define("BASEPATH",$this->BasePath);
		$smarty->assign("BASEPATH",BASEPATH."/");
		$url = str_replace($this->BasePath,"",$url);
		$urlext = pathinfo($url);
		$urlext = $urlext['extension'];
		$url = str_replace(".".$urlext,"",$url);
		if($url[0] == '/'){
			$url = substr($url,1);
		}
		$list = explode("/",$url);//var_dump($list);
		$this->Control = !empty($list[0]) ? strtolower(trim($list[0])) : "index";
		$this->Action = isset($list[1]) ? strtolower(trim($list[1])) : "index";
		$len = count($list);
		if($len > 2){
			for($i = 2 ; $i < $len ;$i++){
				$_GET[$list[$i]] = isset($list[$i + 1]) ? trim($list[$i + 1]) : "";
				$i++;
			}
		}
		foreach($this->_plugins AS $v){
			$plugin = new $v;
			$plugin->run($this);
		}
		$this->Control[0] = strtoupper($this->Control[0]);		
		$fac = $this->_action_dir.'/'.$this->Control.".cls.php";
		if(file_exists($fac)){
			include $fac;
			$con = new $this->Control;
			if(method_exists($con,$this->Action."Action")){
				if($con->{$this->Action."Action"}()){
				}else{
					$smarty->display($this->Control."/".$this->Action.".htm");die();
				}
			}else{echo "$Action";
				$smarty->display("Error/404.htm");die();
			}
		}else{echo "$fac";
			$smarty->display("Error/404.htm");die();
		}
	}
	/**
	 *	设置action的路径
	 **/
	public function setActionDir($path = ''){
		if(file_exists($path)){
			$this->_action_dir = $path;
			return $this;
		}else{
			 echo "没有设置Action路径";die();
		}
	}
	public function setPlugins($name){
		$this->_plugins[] = $name;
		return $this;
	}
	/**
	 *	设置模板路径
	 **/
	public function setTemplatesDir($path = ''){
		if(file_exists($path)){
			$this->_templates_dir = $path;
			return $this;
		}else{
			 echo "没有设置模板路径";die();
		}
	}
	/**
	 *	设置模板缓存路径
	 **/
	public function setTemplatesCacheDir($path = ''){
		if(file_exists($path)){
			$this->_templates_cache_dir = $path;
			return $this;
		}else{
			 echo "没有设置模板缓存路径";die();
		}
	}
	/**
	 *	数据库的设置
	 **/
	public function setDb($host,$dbname,$user,$pass,$charset,$pcon = false){
		$this->_host = $host;
		$this->_dbname = $dbname;
		$this->_user = $user;
		$this->_pass = $pass;
		$this->_charset = $charset;
		$this->_pcon = $pcon;
		return $this;
	}
}
